/*
 *  UI_VolMute.cpp
 *
 *	Date: 2011-4-29
 *  Author: linshenqi
 */

#include "stdafx.h"
#include "./UI_Frame.h"
#include "./CP9550_Protocol.h"
#include "./Configer.h"

extern Configer theCfg;

UI_InputPanel::UI_InputPanel(const CRect *pRect, const GRAPHICPROP *pProp)
	: GraphicBase(pRect, pProp)
	, m_Tag(0)
	, m_nMode(CHN_INPUT)
	, m_bFlag(false)
	, m_sZero("0")
	, m_sMute("Mute")
	, m_ft(RGB(255, 255, 255))
	, m_bRight(false)
	, m_nSel(0)
{
	this->m_Text = "0";

	m_lf.lfCharSet = DEFAULT_CHARSET;
	m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	m_lf.lfEscapement = 0;
	strcpy(m_lf.lfFaceName, FONT_CAPTION);
	m_lf.lfHeight = 20;
	m_lf.lfItalic = true;
	m_lf.lfOrientation = 0;
	m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	m_lf.lfPitchAndFamily = DEFAULT_PITCH;
	m_lf.lfQuality = DEFAULT_QUALITY;
	m_lf.lfStrikeOut = false;
	m_lf.lfUnderline = false;
	m_lf.lfWidth = 0;
	m_lf.lfWeight = 0;
}

bool UI_InputPanel::OnPaint(CPaintDC *pDC, CDC *pMemDC)
{
	//CPaintDC dc(this->m_pWnd);
	//this->SetDC(&dc);
	GRAPHICPROP prop;
	prop.clrFill = COLOR_LINE;
	prop.clrFrame = COLOR_LINE;
	this->DrawLine(&this->m_Line[0].Start, &this->m_Line[0].End, &prop, pDC, pDC);
	this->DrawLine(&this->m_Line[1].Start, &this->m_Line[1].End, &prop, pDC, pDC);
	//this->DrawText(NULL);
	this->DrawText(&m_lf, &this->m_rcft[0], this->m_sZero, COLOR_FONT, pDC, pDC);
	this->DrawText(&m_lf, &this->m_rcft[1], this->m_sMute, COLOR_FONT, pDC, pDC);

	/*for(int i = 0; i < 8; ++i)
	{
		m_VP[i].OnPaint(pDC, pMemDC);
	}*/
	m_VP[0].OnPaint(pDC, pMemDC);
	m_VP[1].OnPaint(pDC, pMemDC);
	m_VP[2].OnPaint(pDC, pMemDC);
	m_VP[3].OnPaint(pDC, pMemDC);
	m_VP[4].OnPaint(pDC, pMemDC);
	m_VP[5].OnPaint(pDC, pMemDC);
	m_VP[6].OnPaint(pDC, pMemDC);
	m_VP[7].OnPaint(pDC, pMemDC);

	GraphicBase::OnPaint(pDC, pMemDC);
	return true;
}

bool UI_InputPanel::OnLButtonDown(UINT nFlags, CPoint point)
{
	for(int i = 0; i < 8; ++i)
	{
		if(m_VP[i].OnLButtonDown(nFlags, point))
		{
			m_Tag = i;
			m_bFlag = true;
			break;
		}
	}

	GraphicBase::OnLButtonDown(nFlags, point);
	return true;
}

bool UI_InputPanel::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_VP[m_Tag].OnLButtonUp(nFlags, point);
	m_bFlag = false;
	GraphicBase::OnLButtonUp(nFlags, point);
	return true;
}

bool UI_InputPanel::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	for(int i = 0; i < 8; ++i)
	{
		m_VP[i].OnLButtonDblClk(nFlags, point);
			//break;
	}
	return true;
}

bool UI_InputPanel::OnRButtonDown(UINT nFlags, CPoint point)
{
	m_bRight = true;
	for(int i = 0; i < 8; ++i)
	{
		if(m_VP[i].OnRButtonDown(nFlags, point))
			break;
	}

	return true;
}

bool UI_InputPanel::OnRButtonUp(UINT nFlags, CPoint point)
{
	m_bRight = !m_bRight;
	for(int i = 0; i < 8; ++i)
	{
		if(m_VP[i].OnRButtonUp(nFlags, point))
			break;
	}

	return true;
}

bool UI_InputPanel::OnMouseMove(UINT nFlags, CPoint point)
{
	//this->SetDC(this->m_pWnd->GetDC());
	//GRAPHICPROP prop;
	//prop.clrFill = IP_COLOR;
	//prop.clrFrame = IP_COLOR;
	//this->DrawRect(this->m_pRect, &prop, REDRAW);
	
	for(int i = 0; i < 8; ++i)
	{
		if(m_bRight)
		{
			if(m_VP[i].OnMouseMove(3, point))
				break;
		}
		else
		{
			if(m_VP[i].OnMouseMove(nFlags, point))
				break;
		}
	}

	if(!m_bFlag)
		return false;

	//m_VP[m_Tag].OnMouseMove(nFlags, point);

	GraphicBase::OnMouseMove(nFlags, point);

	return true;
}

void UI_InputPanel::Redraw()
{
	//GRAPHICPROP prop;
	//prop.clrFill = IP_COLOR;
	//prop.clrFrame = IP_COLOR;
	//this->DrawRect(this->m_pRect, &prop, REDRAW);
}

void UI_InputPanel::SetRect(const CRect *pRect)
{
	GraphicBase::SetRect(pRect);
	
	CRect rc;
	int p = 0;
	int nw = (pRect->Width() - 7 * IP_PADDING) / 8;
	for(int i = 0; i < 8; ++i)
	{
		rc.SetRect(pRect->left + i * (nw + IP_PADDING), pRect->top, pRect->left + i * (nw + IP_PADDING) + nw, pRect->bottom);
		m_VP[i].SetRect(&rc);
		//p = 2;
	}

	this->m_Line[0].Start.y = m_VP[0].m_VS.nTag;
	this->m_Line[0].End.y = m_VP[0].m_VS.nTag;

	this->m_Line[1].Start.y = m_VP[0].m_VS.nTagMute;
	this->m_Line[1].End.y = m_VP[0].m_VS.nTagMute;

	if(m_nMode == CHN_INPUT)
	{
		this->m_Line[0].Start.x = m_VP[0].m_pRect->left - IP_PADDING;
		this->m_Line[0].End.x = m_VP[7].m_pRect->left;
		this->m_Line[1].Start.x = m_VP[0].m_pRect->left - IP_PADDING;
		this->m_Line[1].End.x = m_VP[7].m_pRect->left;
		m_rcft[0].SetRect(this->m_Line[0].Start.x - 24, 
					      this->m_Line[0].Start.y - 12, 
					      this->m_Line[0].Start.x - 16,
					      this->m_Line[0].Start.y - 4);
		m_rcft[1].SetRect(this->m_Line[1].Start.x - 24, 
					      this->m_Line[1].Start.y - 12, 
					      this->m_Line[1].Start.x - 16,
					      this->m_Line[1].Start.y - 4);
	}
	else
	{
		this->m_Line[0].Start.x = m_VP[0].m_pRect->right;
		this->m_Line[0].End.x = m_VP[7].m_pRect->right + IP_PADDING;
		this->m_Line[1].Start.x = m_VP[0].m_pRect->right;
		this->m_Line[1].End.x = m_VP[7].m_pRect->right + IP_PADDING;
		m_rcft[0].SetRect(this->m_Line[0].End.x + 16, 
					      this->m_Line[0].End.y - 12, 
					      this->m_Line[0].End.x + 24,
					      this->m_Line[0].End.y - 4);
		m_rcft[1].SetRect(this->m_Line[1].End.x + 16, 
					      this->m_Line[1].End.y - 12, 
					      this->m_Line[1].End.x + 24,
					      this->m_Line[1].End.y - 4);
	}
}

void UI_InputPanel::SetID(int id)
{
	GraphicBase::SetID(id);

	for(int i = 0; i < 8; ++i)
	{
		m_VP[i].SetID(i);
		m_VP[i].SetParentUI(this);
	}
}

void UI_InputPanel::SetBaseWindow(CWnd *pWnd)
{
	GraphicBase::SetBaseWindow(pWnd);

	for(int i = 0; i < 8; ++i)
	{
		m_VP[i].SetBaseWindow(pWnd);
	}
}

void UI_InputPanel::SetMode(BYTE mode)
{
	if(m_nMode == mode)
		return; 

	m_nMode = mode;
	for(int i = 0; i < 8; ++i)
	{
		m_VP[i].SetMode(mode);
	}
	this->m_pWnd->Invalidate();
}

void UI_InputPanel::Save(const CString& path)
{
	::theCfg.CreateINI(path);
	for(int i = 0; i < 8; ++i)
	{
		m_VP[i].Save(path);
	}
}

void UI_InputPanel::Load(const CString& path)
{
	if(!::theCfg.OpenINI(path))
		return;

	for(int i = 0; i < 8; ++i)
	{
		m_VP[i].Load(path);
	}
	this->m_pWnd->Invalidate();
}